package bepviet.android.engine;

import java.util.ArrayList;

import bepviet.android.properties.StringInPair;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<StringInPair> {

	private ArrayList<StringInPair> arrayList;
	private Context context;
	private String name;
	private int flag;
	private CheckedTextView check;
	private static ArrayList<Integer> mSelectPos;

	public MyArrayAdapter(Context context, ArrayList<StringInPair> arraylist,
			String name) {
		super(context, R.layout.list_items, arraylist);
		this.context = context;
		this.arrayList = arraylist;
		this.name = name;
	}

	public MyArrayAdapter(Context context, ArrayList<StringInPair> arraylist,
			String name, int flag) {
		super(context, R.layout.list_items, arraylist);
		this.context = context;
		this.arrayList = arraylist;
		this.name = name;
		this.flag = flag;
	}

	// public MyArrayAdapter(Context context, ArrayList<StringInPair>
	// arraylist,String name) {
	// super(context, R.layout.list_items ,arraylist);
	// this.context = context;
	// this.arrayList = arraylist;
	// this.name = name;
	// }

	protected View view(final int position,View convertView, ViewGroup parent){
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
		View row = inflater.inflate(R.layout.list_items, parent,false);
		TextView textview = (TextView)row.findViewById(R.id.txtname);
		textview.setText(arrayList.get(position).Loai);			
		ImageView image = (ImageView)row.findViewById(R.id.icon);	
		if(name.equals("MonAn")  ){
			LinearLayout layout = (LinearLayout)row.findViewById(R.id.layout_rating);
			RatingBar rating = (RatingBar)row.findViewById(R.id.ratingbar_Small);
			TextView txtviewer = (TextView)row.findViewById(R.id.txtviewer);
			layout.setVisibility(LinearLayout.VISIBLE);			
		    rating.setRating(arrayList.get(position).Rating);
		    txtviewer.setText((arrayList.get(position).View)+" view");
		
		}
		if(name == "MonAn")
			image.setImageResource(R.drawable.food2);
		else if(name == "TheLoai")
			image.setImageResource(R.drawable.food);
		else if(name == "XuatXu")
			image.setImageResource(R.drawable.from);
		else 
			image.setImageResource(R.drawable.material);
		
		
		//===================================check item ============================================
		check = (CheckedTextView)row.findViewById(R.id.check_item);		
		mSelectPos = new ArrayList<Integer>();
		
		if(flag==1)
			check.setVisibility(CheckedTextView.VISIBLE);
		try{
		check.setChecked(mSelectPos.contains(new Integer(position)));
		}
		catch(Exception ex){ Log.v("tag", ex.getMessage());}
		check.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((CheckedTextView)v).toggle();
				if (((CheckedTextView) v).isChecked())
					mSelectPos.add(new Integer(position));
				else
					mSelectPos.remove(new Integer(position));				
			}
		});
		
		return row;
	}

	public void DeleteItem(String TABLE) {
		
		for (int i = mSelectPos.size() - 1; i >=0 ; i--) {
			StringInPair item = (StringInPair)getItem(mSelectPos.get(i));	
			DataFoodAdapter dataAdapter = new DataFoodAdapter(context);
			SQLiteDatabase db = dataAdapter.OpenPath();
			dataAdapter.Delete(db, Integer.toString(item.getMa()), TABLE);
			this.remove(item);
			dataAdapter.Close(db);
		}
		this.notifyDataSetChanged();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		return view(position, convertView, parent);
	}

}
